package com.example.szh.nextwork.presenter;

import com.example.szh.entity.BaseEntity;
import com.example.szh.nextwork.model.IModel;
import com.example.szh.nextwork.view.IView;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public abstract class BasePresenter<M extends IModel,V extends IView> implements IPresenter, Observer<BaseEntity> {

    private CompositeDisposable compositeDisposable;//RxJAVA下专门用来保存
    //protected 不同的子类包可见
    protected M m;
    protected V v;

    public BasePresenter(M m,V v){
        this.m=m;
        this.v=v;
        compositeDisposable=new CompositeDisposable();
    }
    //RXjava在订阅成功后的disposable的参数，当异步任务未执行完成，切界面被用户手动关闭
    //在通过lifecycle关联的声明周期方法onDestroy中接触rxjava异步订阅任务防止内存泄漏

    @Override
    public void Destory() {
        //解除全部异步订阅任务
        compositeDisposable.dispose();
        compositeDisposable.clear();
    }

    @Override
    public void onSubscribe(@NonNull Disposable d) {
        compositeDisposable.add(d);
        v.showDialog();
    }


    @Override
    public void onError(@NonNull Throwable e) {
        v.showMsg(e.getMessage());
    }

    @Override
    public void onComplete() {
        v.hideDialog();
    }
}
